/*
 * DialogRules
 * Version: 0.1
 * Date: 2009-03-01
 * Author: gp08-nhn
 */

package keyboardhero;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
import java.io.IOException;

final class DialogRules extends AbstractDialog {
	
	//-------------------- VARIABLES --------------------
	
	private static final long serialVersionUID = 5983805921440757485L;
	
	private static DialogRules instance = null; // used only for debugging
	
	private JEditorPane editorPane;
	private boolean needDocumentSetup = true;
	
	//-------------------- CORE FUNCTIONS --------------------
	
	DialogRules(JFrame frame) {
		super(frame, "Menu_Rules", "rules", true);
		
		final Container contentPane;
		(contentPane = getContentPane()).add(new JScrollPane(editorPane = new JEditorPane()), BorderLayout.CENTER);
		
		editorPane.setEditable(false);
		
		final JPanel panel;
		contentPane.add(panel = new JPanel(), BorderLayout.SOUTH);
		panel.setLayout(new FlowLayout());
		
		final JButton bnOk;
		panel.add(bnOk = new JButton(Util.getMsgMnemonic("OK")));
		bnOk.setMnemonic(Util.getLastMnemonic());
		bnOk.setDisplayedMnemonicIndex(Util.getLastMnemonicIndex());
		bnOk.setName("OK");
		bnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close();
			} // end method
		} // end class
		);
		getRootPane().setDefaultButton(bnOk);
		
		instance = this;
	}
	
	private void documentSetup() {
		final String lang = Util.utf2ascii(Util.getLang());
		final String defLang = Util.utf2ascii(Util.getDefLang());
		
		URL rulesURL = Util.getURL("Manual." + lang + ".html");
		Util.debug(rulesURL);
		if(rulesURL == null) {
			if(!lang.equals(defLang)) {
				rulesURL = Util.getURL("Manual." + defLang + ".html");
			}
			if(rulesURL == null && !lang.equals("English") && !defLang.equals("English")) {
				rulesURL = Util.getURL("Manual.English.html");
			}
		}
		
		if(rulesURL != null) {
			try {
				editorPane.setPage(rulesURL);
				new JEditorPane();
			} catch(IOException e) {
				editorPane.setText("Attempted to read a bad URL: " + rulesURL + "\n\n" + e.toString());
			}
		} else {
			editorPane.setText("No appropraite rules file found (i.e.: Manual." + lang + ".html)!");
		}
		
		needDocumentSetup = false;
	}
	
	//-------------------- OVERRIDING FUNCTIONS --------------------
	
	@Override
	protected void open() {
		if(needDocumentSetup) documentSetup();
		super.open();
	} // end method
	
	@Override
	protected void updateTexts() {
		super.updateTexts();
		needDocumentSetup = true;
	}
	
	@Override
	protected AbstractDialog getNewInstance(JFrame frame) {
		return new DialogRules(frame);
	}
	
	//-------------------- DEBUGGING FUNCTIONS --------------------
	
	/**
	 * Creates a string containing the most important information about the game. This method is
	 * used only for debugging and testing purposes.
	 * 
	 * @return the created string.
	 */
	static String getString() { //$SUP-MCP$
		return "DialogRules(visible=" + instance.isVisible() + "; location=" + instance.getLocationOnScreen() + "; bounds=" + instance.getBounds() + ")";
	} // end method
	
	/**
	 * This method serves security purposes. Provides an integrity string that will be checked by
	 * the {@link Connection#integrityCheck()} method; thus the application can only be altered if
	 * the source is known. Every class in the {@link keyboardhero} package has an integrity string.
	 * 
	 * @return the string of this class used for integrity checking.
	 */
	static String getIntegrityString() {
		return "345\nSDf+!kj+JKs1fOgđ";
	} // end method
	
	/**
	 * The tester object of this class. It provides a debugging menu and unit tests for this class.
	 * Its only purpose is debugging or testing.
	 */
	final static Tester TESTER = new Tester("DialogRules", new String[] {"getString()"} /* end initialization */) { //$SUP-PPA$
		void menu(int choice) throws Exception {
			switch(choice) {
				case 5:
					System.out.println(getString());
					break;
				default:
					baseMenu(choice);
					break;
			} // end switch
		} // end method
		
		void runUnitTests() throws Exception {
			higherTestStart("DialogRules");
			
			testEq("getIntegrityString()", "345\nSDf+!kj+JKs1fOgđ", DialogRules.getIntegrityString());
			
			higherTestEnd();
		} // end method
	}; // end class
	
	/**
	 * Starts the class's developing menu. If this build is a developer's one it starts the
	 * application in a normal way with the exception that it starts the debugging tool for this
	 * class as well; otherwise exits with an error message.
	 * 
	 * @param args
	 *            the arguments given to the program.
	 * @see KeyboardHero#startApp()
	 */
	public static void main(String[] args) {
		Tester.mainer(args, TESTER);
	} // end method
}
